<template>
  <div>

    <h2>{{ id ? '编辑英雄':'新增英雄' }}</h2>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-tabs type="border-card">
        <el-tab-pane label="基础信息">
          <el-form-item label="名称">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <el-form-item label="称号">
            <el-input v-model="model.title"></el-input>
          </el-form-item>
          <el-form-item label="头像">

          </el-form-item>
          <el-form-item label="Banner">

          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="难度">
            <el-rate style="margin-top:0.6rem" :max="9" show-score></el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate style="margin-top:0.6rem" :max="9" show-score ></el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate style="margin-top:0.6rem" :max="9" show-score ></el-rate>
          </el-form-item>
          <el-form-item label="生存">
            <el-rate style="margin-top:0.6rem" :max="9" show-score></el-rate>
          </el-form-item>

        </el-tab-pane>
        <el-tab-pane label="技能">技能</el-tab-pane>
        <el-tab-pane label="最佳拍档">最佳拍档</el-tab-pane>
      </el-tabs>
    </el-form>

  </div>

</template>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<script>
    export default {
      data() {
        return {
          model:{},
          id:this.$route.query.id,
          imageUrl: '',
          colors: ['#99A9BF', '#F7BA2A', '#FF9900']  // 等同于 { 2: '#99A9BF', 4: { value: '#F7BA2A', excluded: true }, 5: '#FF9900' }

        }

      },
    methods : {
        //保存
       async save() {
         let res
         if(this.id != null){
           res = await this.$http.post('/cms/items/edit.json/'+this.id, this.model)
         }
         else {
           res = await this.$http.post('/cms/items/add.json', this.model)
         }
          //跳转到list页面
          this.$router.push("/categories/list")
          this.$message({
             type: "success",
             message: '保存成功！'
          })
       },

      //数据回显
      async fetch() {
          const  res = await this.$http.get("/cms/items/view.json/"+this.id)
          this.model = res.data.data
       },

      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }

    },
   created() {
        this.id && this.fetch()
      }
    }
</script>

<style scoped>

</style>
